package com.learnkitchen.learnkitchenrecipes.base;

import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.learnkitchen.learnkitchenrecipes.ProgressFragment;
import com.learnkitchen.learnkitchenrecipes.R;
import com.learnkitchen.learnkitchenrecipes.utils.KeyboardUtil;
import com.learnkitchen.learnkitchenrecipes.utils.ToastUtil;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;

import java.util.List;

/**
 * Created by Administrator on 2018-01-03.
 */

public abstract class BaseActivity extends AppCompatActivity {

    public static final int REQUEST_CODE_PERMISSION = 100;
    public static final int REQUEST_CODE_SETTING = 300;
    /**
     * 全屏管理
     */
    private ImmersionBarManager barManager;
    private ProgressFragment progressFragment;

    /**
     * 显示标题头
     *
     * @param title
     */
    public void initToolBar(String title) {
        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView mToolbarTitle = (TextView) findViewById(R.id.toolbar_title);
        mToolbarTitle.setText(title);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setSceenLight();
        setFullScreen();

        super.onCreate(savedInstanceState);
        ActivityManager.getInstance().pushActivity(this);
    }

    /**
     * 子类重写这个方法true:允许向右滑动，false:禁止向右滑动
     * @return
     */
    protected boolean enableRightSliding(){
        return true;
    }

    /**
     * 增加沉浸式状态栏
     * @return
     */
    protected boolean addStatuebar(){
        return true;
    }


    protected void initPermission(String[] permisions) {
        AndPermission.with(this)
                .requestCode(REQUEST_CODE_PERMISSION)
                .permission(permisions)
                .callback(permissionListener)
                .start();
    }

    /**
     * 保持屏幕亮,不息屏
     */
    private void setSceenLight() {
        if (isScreenLight()) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    }

    /**
     * 显示全屏
     */
    private void setFullScreen() {
        if (isFullScreen()) {
            barManager = new ImmersionBarManager(this);
            barManager.transparentStatusBar(true);
        }
    }

    /**
     * 默认不显示全屏
     *
     * @return
     */
    public boolean isFullScreen() {
        return true;
    }

    /**
     * 默认不显示全屏
     *
     * @return
     */
    public boolean isScreenLight() {
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        ActivityManager.getInstance().popActivity(this);
        hideLoading();
        if (barManager != null) {
            barManager.onDestroy();
            barManager = null;
        }
        super.onDestroy();
    }


    @Override
    public void onBackPressed() {
        KeyboardUtil.hideSoftInput(BaseActivity.this);
        finish();
    }

    public void showToast(String msg) {
        ToastUtil.show(this,msg);
    }

    /**
     * 显示等待对话框
     */
    public void showLoading(String textString) {
        Bundle bundle = new Bundle();
        bundle.putString("loading", textString);
        // 显示进度对话框
        progressFragment = new ProgressFragment();
        progressFragment.setCancelable(true);
        progressFragment.setArguments(bundle);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(progressFragment, "progressFragment");
        ft.commitAllowingStateLoss();
    }

    /**
     * 显示等待对话框
     * 不能点击外面 消失dialog
     */
    public void showNoCancelLoading(String textString) {
        Bundle bundle = new Bundle();
        bundle.putString("loading", textString);
        // 显示进度对话框
        progressFragment = new ProgressFragment();
        progressFragment.setCancelable(false);
        progressFragment.setArguments(bundle);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(progressFragment, "progressFragment");
        ft.commitAllowingStateLoss();
    }

    /**
     * 关闭进度对话框
     */
    public void hideLoading() {
        // 关闭进度对话框
        if (progressFragment != null) {
            progressFragment.dismissAllowingStateLoss();
        }
    }


    /**
     * 关闭所有Activity
     */
    public void finishAllActivity() {
        ActivityManager.getInstance().finishAllActivity();
    }

    /**
     * 获取权限成功
     */
    protected void permissionYes() {

    }

    /**
     * 权限回调监听。
     */
    private PermissionListener permissionListener = new PermissionListener() {
        @Override
        public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
            switch (requestCode) {
                case REQUEST_CODE_PERMISSION: {
                    permissionYes();
                    break;
                }
            }
        }

        @Override
        public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
            switch (requestCode) {
                case REQUEST_CODE_PERMISSION: {
                    permissionNo();
                    break;
                }
            }
        }
    };

    /**
     * 获取权限失败
     */
    protected void permissionNo() {

    }

    protected void isShowPermissionDialog() {
        AndPermission.defaultSettingDialog(this, REQUEST_CODE_SETTING).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
            }
        }).show();
    }

}
